package com.zhanghp.class05.compare;

import com.zhanghp.utils.CommonUtils;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author zhanghp
 * @since 2023/11/18 15:35
 */
public class ShowComparator2 {
    public static void main(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        queue.add(4);
        queue.add(1);
        queue.add(2);
        System.out.println(queue.peek());
        CommonUtils.printDivingLine();
        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
        CommonUtils.printDivingLine();
        PriorityQueue<Integer> queue2 = new PriorityQueue<>(new MyComparator());
        queue2.add(4);
        queue2.add(1);
        queue2.add(2);
        while (!queue2.isEmpty()) {
            System.out.println(queue2.poll());
        }

        CommonUtils.printDivingLine();
        PriorityQueue<Student> heap = new PriorityQueue<>(new ShowComparator.IdCompare());
        Student s1 = new Student("张三", 5, 27);
        Student s2 = new Student("李四", 1, 17);
        Student s3 = new Student("王五", 4, 29);
        Student s4 = new Student("赵六", 3, 9);
        Student s5 = new Student("左七", 2, 34);
        heap.add(s1);
        heap.add(s2);
        heap.add(s3);
        heap.add(s4);
        heap.add(s5);
        while (!heap.isEmpty()) {
            Student s = heap.poll();
            System.out.println(s.name + ", " + s.id + ", " + s.age);
        }

        CommonUtils.printDivingLine();
        // 字典序
        String a = "abc";
        String b = "ab";
        // a 大于 b --> abc ab0
        // b若和a长度不一，则用ASCII的0补位
        System.out.println(a.compareTo(b));

    }

    public static class MyComparator implements Comparator<Integer>{

        @Override
        public int compare(Integer o1, Integer o2) {
            if (o1 > o2) {
                return -1;
            }else if (o1 < o2){
                return 1;
            }else {
                return 0;
            }
        }
    }
}
